var EventsList = function(element, options , callback) {
	var $main = $('#wrapper');
	var $list = $main.find('#record-list');
	var $pullDown = $main.find('#pull-down');
	//var $pullDownLabel = $main.find('#pull-down-label');
	var $pullUp = $main.find('#pull-up');
	var $pullUpLabel = $main.find('#pull-up-label');
	var topOffset = -$pullDown.outerHeight();
	var winHeigth = $(window).height();
	
	var isClick = false;
	if(options.isClick == true){
		isClick = true;
	}

	this.prev = this.next = this.page = options.params.page;
	this.total = null;

	this.getURL = function(params) {
		var queries = [];
		for (var key in  params) {
			if (key !== 'page'&&key !== 'no_record') {
				queries.push(key + '=' + params[key]);
			}
		}
		queries.push('page=');
		return options.api + '?' + queries.join('&');
	};

	this.renderList = function(page, type) {
		var _this = this;
		var $el = $pullDown;

		if (type === 'load') {
			$el = $pullUp;
		}

		$.getJSON(this.URL + page).then(function(data) {
			_this.total = data.total;
			if(data.total==0){
				if(options.params.no_record == undefined)
					$('.am-list-item-text').addClass('am-list-no-item-text').text("暂无记录");
				else
					$('.am-list-item-text').addClass('am-list-no-item-text').text(options.params.no_record);
				$list.css("min-height",winHeigth);
				//$pullDown.hide();
				$pullUp.hide();
			}else{
				if(data.total==1){
					$pullUpLabel.text("没有更多数据了");
					$list.css("min-height",winHeigth);
					//暂时解决
					//注：对于scroll的滚动区域小于包裹容器的，iscroll禁止滚动，
					//会造成无法实现下拉刷新，这里有一个小技巧，就是给滚动区域加一个min-height;
				}
				if (type !== 'load') {
					$list.empty();
				}
				callback(data,type,$list);
			}
			// refresh iScroll
			setTimeout(function() {
				_this.iScroll.refresh();
			}, 100);
		}, function() {
		}).always(function() {
			_this.resetLoading($el);
			if (type !== 'load') {
				_this.iScroll.scrollTo(0, topOffset, 800, $.AMUI.iScroll.utils.circular);
			}
		});
	};

	this.setLoading = function($el) {
		$el.addClass('loading');
	};

	this.resetLoading = function($el) {
		$el.removeClass('loading');
	};

	this.init = function() {
		var myScroll = this.iScroll = new $.AMUI.iScroll('#wrapper', {
			//有bug，true时有些页面会出现双击
			click: isClick
		});
		//myScroll.scrollTo(0, topOffset);
		var _this = this;
		var pullFormTop = false;
		var pullStart;

		this.URL = this.getURL(options.params);
		this.renderList(options.params.page,"init");

		myScroll.on('scrollStart', function() {
			if (this.y >= topOffset) {
				pullFormTop = true;
			}

			pullStart = this.y;
			// console.log(this);
		});

		myScroll.on('scrollEnd', function() {
			if (pullFormTop && this.directionY === -1) {
				_this.handlePullDown();
			}
			pullFormTop = false;

			// pull up to load more
			if (pullStart === this.y && (this.directionY === 1)) {
				_this.handlePullUp();
			}
		});
	};

	this.handlePullDown = function() {
		this.setLoading($pullDown);
		if (this.prev > 1) {
			this.prev -= 1;
		} else {
			this.prev = 1;
		}
		this.renderList(this.prev, 'refresh');
	};

	this.handlePullUp = function() {
		if (this.next < this.total) {
			this.setLoading($pullUp);
			this.next += 1;
			this.renderList(this.next, 'load');
		} else {
			$pullUpLabel.text("没有更多数据了");
		}
	};
};